﻿/**
* jQuery+xml省市县三级联动
*
* 支持单选省及省市二级联动
*
* 调用时请先包含jquery库
*
* @author	Joseph.Yang
* @version 1.0
*
* 用法示例：
*	var params = {
*		province: "sel_province",//选择省控件ID
*		city: "sel_city",//选择市控件ID
*		county: "sel_county"//选择县控件ID
*	};
*	var dataParams = {
*		province: "北京",//选中指定省
*		city: "北京",//选中指定市
*		county: "海淀区"//选中指定县
*	};
*	$.AreaOption(params,dataParams);
*/

/**
* 读取xml文件
* @param xml string xml文件路径
*/
function loadXml(XmlSrc) {
    var xmlDom = null;
    if (window.ActiveXObject) //IE
    {
        xmlDom = NewDomObj();
        xmlDom.async = false;
        xmlDom.load(XmlSrc);
        if (xmlDom.parseError.errorCode != 0) {
            xmlDom = null;
        }
    }
    else if (document.implementation && document.implementation.createDocument) //FireFox,Opera
    {
        xmlDom = document.implementation.createDocument("", "", null);
        xmlDom.async = false;
        try {
            xmlDom.load(XmlSrc);
        }
        catch (ex) {//Safari,Chorme
            xmlDom = null;
        }
    }
    else {
        window.alert(td_lang.crm.inc.msg_47);
        return null;
    }

    if (xmlDom == null) {
        var req = new XMLHttpRequest();
        req.open("GET", XmlSrc, false);
        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                xmlDom = req.responseXML;
            }
        };
        req.send(null);
    }

    return xmlDom;
};

function NewDomObj() {
    var DomType = new Array("microsoft.xmldom", "msxml.domdocument", "msxml2.domdocument", "msxml2.domdocument.3.0", "msxml2.domdocument.4.0", "msxml2.domdocument.5.0");
    for (var i = 0; i < DomType.length; i++) {
        try {
            var a = new ActiveXObject(DomType[i]);
            if (!a) continue;
            return a;
        }
        catch (ex) { }
    }
    return null;
}


/**
* 扩展FF兼容selectNodes方法
*/
if (!window.ActiveXObject) {
    (function () {
        var oEvaluator = new XPathEvaluator(), oResult;
        XMLDocument.prototype.selectNodes = function (sXPath) {
            oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            var aNodes = [];
            if (oResult != null) {
                var oElement = oResult.iterateNext();
                while (oElement) {
                    aNodes[aNodes.length] = oElement;
                    oElement = oResult.iterateNext();
                }
            }
            return aNodes;
        }
    })()
}

(function ($) {
    $.extend({
        AreaOption: function (params, dataParams) {
            if (typeof params == "undefined") {
                return false;
            }
            if (typeof params.province == "undefined" || $("#" + params.province).length == 0) {
                return false;
            }
            var xml = g_PLATFORM_PATH + "/js/general/area/area.xml";
            var xmlDoc = loadXml(xml);
            var provinceNodes = xmlDoc.selectNodes("/area/province");
            var str = "";
            str += "<option value=\"\">" + $("#" + params.province).find("option:first").html() + "</option>";
            $("#" + params.province).html("");
            for (var i = 0; i < provinceNodes.length; i++) {
                var name = provinceNodes[i].getAttribute("name");
                var code = provinceNodes[i].getAttribute("code");
                str += "<option value=\"" + name + "\" code=\"" + code + "\">" + name + "</option>";
            }
            $("#" + params.province).html(str);

            $("#" + params.province).change(function () {
                if (typeof params.city == "undefined" || $("#" + params.city).length == 0) {
                    return false;
                }
                var code = $("#" + params.province).find("option:selected").attr("code");
                var cityNodes = xmlDoc.selectNodes("/area/province[@code='" + code + "']/city");
                var str = "";
                str += "<option value=\"\">" + $("#" + params.city).find("option:first").html() + "</option>";
                $("#" + params.city).html("");
                for (var i = 0; i < cityNodes.length; i++) {
                    var name = cityNodes[i].getAttribute("name");
                    var code = cityNodes[i].getAttribute("code");
                    str += "<option value=\"" + name + "\" code=\"" + code + "\">" + name + "</option>";
                }
                $("#" + params.city).html(str);
                $("#" + params.city).change();
            });

            $("#" + params.city).change(function () {
                if (typeof params.county == "undefined" || $("#" + params.county).length == 0) {
                    return false;
                }
                var provinceCode = $("#" + params.province).find("option:selected").attr("code");
                var cityCode = $("#" + params.city).find("option:selected").attr("code");
                var countyNodes = xmlDoc.selectNodes("/area/province[@code='" + provinceCode + "']/city[@code='" + cityCode + "']/county");
                var str = "";
                str += "<option value=\"\">" + $("#" + params.city).find("option:first").html() + "</option>";
                $("#" + params.county).html("");
                for (var i = 0; i < countyNodes.length; i++) {
                    var name = countyNodes[i].getAttribute("name");
                    str += "<option value=\"" + name + "\">" + name + "</option>";
                }
                $("#" + params.county).html(str);
            });

            if (typeof dataParams != "undefined") {
                if (typeof dataParams.province != "undefined" && $("#" + params.province).length > 0) {
                    if (typeof $("#" + params.province).find("option[value='" + dataParams.province + "']").val() == "undefined") {
                        var oOption = document.createElement("OPTION");
                        $("#" + params.province)[0].options.add(oOption);
                        oOption.innerText = dataParams.province;
                        oOption.value = dataParams.province;
                    }
                    $("#" + params.province).val(dataParams.province);
                    $("#" + params.province).change();

                    if (typeof dataParams.city != "undefined" && $("#" + params.city).length > 0) {
                        if (typeof $("#" + params.city).find("option[value='" + dataParams.city + "']").val() == "undefined") {
                            var oOption = document.createElement("OPTION");
                            $("#" + params.city)[0].options.add(oOption);
                            oOption.innerText = dataParams.city;
                            oOption.value = dataParams.city;
                        }
                        $("#" + params.city).val(dataParams.city);
                        $("#" + params.city).change();

                        if (typeof dataParams.county != "undefined" && $("#" + params.county).length > 0) {
                            if (typeof $("#" + params.county).find("option[value='" + dataParams.county + "']").val() == "undefined") {
                                var oOption = document.createElement("OPTION");
                                $("#" + params.county)[0].options.add(oOption);
                                oOption.innerText = dataParams.county;
                                oOption.value = dataParams.county;
                            }
                            $("#" + params.county).val(dataParams.county);
                        }
                    }
                }
            }
        }
    });
})(jQuery);	